// packageJ/chain-store/chainStoreSearchStore/chainStoreSearchStore.js
const app = getApp();
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    keyActive: 1,
    city: "",
    address: "",
    point: "",
    info: [],
    show: false,
    myLocation: "",

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let myLocation = wx.getStorageSync('address-point');
    console.log(myLocation)
    if (myLocation) {
      this.setData({
        city: myLocation.city,
        point: myLocation.point
      })
      this.getData()
    } else {
      this._getLocation((res) => {
        this.setData({
          city: res.ad_info.city,
          point: res.ad_info.location
        })
        this.getData();
      }, (err) => {
        this.getData();
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore && this.data.keyActive == 1) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  childConfirm(){
    this.getData();
  },
  editAddress(e) {
    console.log(e)
    let {item} = e.currentTarget.dataset;
    console.log(item)
    let yzaddress = this.selectComponent('#yz_showAddressShop');
    let josn = {
      currentTarget: {
        dataset: {
          item: item
        }
      }
    }
    yzaddress.setData({
      chainStore:true
    })
    yzaddress.editAddress(josn);
    console.log('editAddress', yzaddress)
  },
  AddshowTap(){
    let yzaddress = this.selectComponent('#yz_showAddressShop');
    yzaddress.setData({
      chainStore:true,
    })
    yzaddress.showAddAddress();
  },
  goLoaction() {
    wx.navigateTo({
      url: '/packageJ/chain-store/chainStoreLoaction/chainStoreLoaction?tag=chainStore',
    })
  },
  tapSelectStore(e) {
    let {
      item
    } = e.currentTarget.dataset;
    let json = {
      get_type: this.data.keyActive,
      lng: this.data.point.lng,
      lat: this.data.point.lat
    };
    if (this.data.keyActive != 1) {
      json.address_id = item.id;
      wx.redirectTo({
        url: `/packageJ/chain-store/chainStoreIndex/chainStoreIndex?get_type=${json.get_type}&lng=${json.lng}&lat=${json.lat}&address_id=${json.address_id}`,
      })
    } else {
      json.store_id = item.id;
      wx.redirectTo({
        url: `/packageJ/chain-store/chainStoreIndex/chainStoreIndex?get_type=${json.get_type}&lng=${json.lng}&lat=${json.lat}&store_id=${json.store_id}`,
      })
    }
  },
  callStore(e) {
    let {
      item
    } = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: item.mobile,
    })
  },
  goToAdress(e) {
    let {
      item
    } = e.currentTarget.dataset;
    let latitude = item.latitude;
    let longitude = item.longitude;
    let store_name = item.store_name;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name: store_name
    });
  },
  toPostData() {
    this.setData({
      info: [],
    })
    console.log(this.data.keyword)
    this.getData();
  },
  tapNav(e) {
    let {
      key
    } = e.currentTarget.dataset;
    this.setData({
      keyActive: key,
      info: []
    })
    this.getData()
  },
  getData() {
    let urlstr = "";
    if (this.data.keyActive == 1) {
      urlstr = app.getNetAddresss("plugin.chain-store.frontend.controller.index.store-list");
    } else {
      urlstr = app.getNetAddresss("member.member-address.index");
    }
    app._postNetWork({
      url: urlstr,
      data: {
        keyword: this.data.keyword,
        lng: this.data.point.lng,
        lat: this.data.point.lat,
        city_name: this.data.city,
        page: this.data.page
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          if (this.data.keyActive == 1) {
            this.setData({
              info: res.data.list.data,
              isLoadMore: true,
              total_page: res.data.list.last_page
            })
            console.log(this.data.info, this.data.total_page)
          } else {
            this.setData({
              info: res.data
            })
          }
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      urlStr = app.getNetAddresss('plugin.chain-store.frontend.controller.index.store-list');
      app._postNetWork({
        url: urlStr,
        data: {
          keyword: this.data.keyword,
          lng: this.data.point.lng,
          lat: this.data.point.lat,
          city_name: this.data.city,
          page: this.data.page
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              info: this.data.info.concat(res.data.list.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})